package cn.elricyu.tt.action.tech;

import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.elricyu.tt.action.base.AbstractAction;
import cn.elricyu.tt.bean.User;
import cn.elricyu.tt.bean.officer.MyOfficer;
import cn.elricyu.tt.bean.tech.MyTech;
import cn.elricyu.tt.bean.tech.Technology;
import cn.elricyu.tt.util.JsonUtil;

public class TechMainAction extends AbstractAction<MyOfficer> {
	private static Log logger = LogFactory.getLog(TechMainAction.class);
	private User self = null;

	public TechMainAction(User self) {
		this.self = self;
		this.url = "root/tech!getTech.action";
	}

	public void process(JSON json) {
		MyTech myTech = self.getMyTech();
		if (myTech == null) {
			myTech = new MyTech();
			self.setMyTech(myTech);
		}
		doParseAll(myTech, json);
	}

	public static void doParseAll(MyTech myTech, JSON json) {
		try {
			long cd = JsonUtil.getLongValue(json, "cd", 0l);
			int cdflag = JsonUtil.getIntValue(json, "cdflag", 0);
			myTech.setCd(cd);
			myTech.setCdflag(cdflag);
			List<JSONObject> technologyList = JsonUtil.getListJsonValue(json,
					"technology");
			for (JSONObject technologyObj : technologyList) {
				String techid = JsonUtil.getStringValue(technologyObj,
						"techid", "");
				String techname = JsonUtil.getStringValue(technologyObj,
						"techname", "");
				String intro = JsonUtil.getStringValue(technologyObj, "intro",
						"");
				int jyungong = JsonUtil.getIntValue(technologyObj, "jyungong",
						0);
				int techlevel = JsonUtil.getIntValue(technologyObj,
						"techlevel", 0);
				int upgradelevel = JsonUtil.getIntValue(technologyObj,
						"upgradelevel", 0);
				int maxtechlevel = JsonUtil.getIntValue(technologyObj,
						"maxtechlevel", 0);
				int cdtime = JsonUtil.getIntValue(technologyObj, "cdtime", 0);
				int order = JsonUtil.getIntValue(technologyObj, "order", 0);
				Technology technology = myTech.getTechnology(techid);
				if (technology == null) {
					technology = new Technology();
					technology.setTechid(techid);
					myTech.addTech(technology);
				}
				technology.setTechname(techname);
				technology.setIntro(intro);
				technology.setJyungong(jyungong);
				technology.setTechlevel(techlevel);
				technology.setUpgradelevel(upgradelevel);
				technology.setMaxtechlevel(maxtechlevel);
				technology.setCdtime(cdtime);
				technology.setOrder(order);
			}

		} catch (Exception e) {
		}
	}

}
